package com.rd.student.jike.class2.lihongming.week731_806.Day0803.Test;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @Auther:Heinrich
 * @Date:2023/8/3-08-03-15:37
 * @Description:com.rd.student.jike.class2.lihongming.Day0803.Test
 * @version:1.0
 */
public class AgeSort implements Comparable{
    int age ;

    public int getAge() {
        return age;
    }

    public AgeSort() {
    }

    public AgeSort(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "AgeSort{" +
                "age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        AgeSort ageSort = (AgeSort) o;
        if(this.age > ageSort.age){
            return 1;
        } else if (this.age < ageSort.age) {
            return -1;
        }
        return 0;
    }
}
class Test01{
    public static void main(String[] args) {
        ArrayList<AgeSort> arrayList = new ArrayList<>(4);
        arrayList.add(new AgeSort(18));
        arrayList.add(new AgeSort(50));
        arrayList.add(new AgeSort(25));
        arrayList.add(new AgeSort(15));
        System.out.println(arrayList);
        ConBubbleSort(arrayList);
        System.out.println(arrayList);
        Collections.shuffle(arrayList);
        System.out.println(arrayList);
        ConQuickSort(arrayList,0,arrayList.size()-1);
        System.out.println(arrayList);
    }
    public static void ConBubbleSort(ArrayList<AgeSort> arrayList){
        int exchange = arrayList.size();
        int size = arrayList.size()-1;
        while (exchange >= 1){
            exchange = 0;
            for (int i = 0; i < size; i++) {
                if(arrayList.get(i).compareTo(arrayList.get(i + 1)) > 0){
                    //Collections.swap(arrayList,i,i+1);
                    arrayList.set(i,arrayList.set(i+1,arrayList.get(i)));
                    exchange = i;
                }
            }
            size = exchange;
        }
    }

    public static void ConQuickSort(ArrayList<AgeSort> arrayList, int low, int high) {
        //递归出口
        if (low > high) return;
        int i = low, j = high;
        AgeSort ageSort = arrayList.get(i);
        while (i < j) {
            while (ageSort.compareTo(arrayList.get(j)) <= 0 && i < j) {
                j--;
            }
            arrayList.set(i, arrayList.get(j));
            while (ageSort.compareTo(arrayList.get(i)) >= 0 && i < j) {
                i++;
            }
            arrayList.set(j, arrayList.get(i));
        }
        arrayList.set(i, ageSort);
        ConQuickSort(arrayList, low, i - 1);
        ConQuickSort(arrayList, j + 1, high);
    }


}